package com.atguigu.gmall.common.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: 封捷
 * @create-date: 2022/9/6 14:50
 */

@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {

        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        // 1、获取 request 对象
        HttpServletRequest request = requestAttributes.getRequest();

        // 2、给 request 对象设置请求消息头
        // [1]正式用户 id
        requestTemplate.header("userId", request.getHeader("userId"));

        // [2]临时用户 id
        requestTemplate.header("userTempId", request.getHeader("userTempId"));

    }
}
